//
//  Widget.swift
//  Widget
//
//  Created by xxs on 2024/10/21.
//

import WidgetKit
import SwiftUI
import WidgetCalendarShared

struct Provider: TimelineProvider {
    func placeholder(in context: Context) -> SimpleEntry {
        SimpleEntry(date: Date(), emoji: "😀")
    }

    func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {
        let entry = SimpleEntry(date: Date(), emoji: "😀")
        completion(entry)
    }

    func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
        var entries: [SimpleEntry] = []

        // Generate a timeline consisting of five entries an hour apart, starting from the current date.
        let currentDate = Date()
        for hourOffset in 0 ..< 5 {
            let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
            let entry = SimpleEntry(date: entryDate, emoji: "😀")
            entries.append(entry)
        }

        let timeline = Timeline(entries: entries, policy: .atEnd)
        completion(timeline)
    }

//    func relevances() async -> WidgetRelevances<Void> {
//        // Generate a list containing the contexts this widget is relevant in.
//    }
}

struct SimpleEntry: TimelineEntry {
    let date: Date
    let emoji: String
}

struct WidgetEntryView : View {
    var entry: Provider.Entry

    var body: some View {
        VStack {
            Text("Time:")
            Text(entry.date, style: .time)

            Text("Emoji:")
            Text(entry.emoji)
        }
    }
}

struct MonthWidget: Widget {
    let kind: String = "MonthWidget"

    var body: some WidgetConfiguration {
        IntentConfiguration(kind: kind, intent: SmallMonthProvider.Intent.self, provider: SmallMonthProvider()) { entry in
            SmallMonthView(entry: entry, todayIntent: TodayIntent(), lastIntent: LastMonthIntent(), nextIntent: NextMonthIntent())
        }
        .configurationDisplayName("My Widget")
        .description("This is an example widget.")
        .supportedFamilies([.systemSmall, .systemMedium, .systemLarge, .systemExtraLarge])
    }
}

struct TabooWidget: Widget {
    let kind: String = "TabooWidget"

    var body: some WidgetConfiguration {
        IntentConfiguration(kind: kind, intent: SmallMonthProvider.Intent.self, provider: SmallMonthProvider()) { entry in
            SmallTabooView(entry: entry, todayIntent: TodayIntent(), lastIntent: LastMonthIntent(), nextIntent: NextMonthIntent())
        }
        .configurationDisplayName("L10n.Localizable.Widget.Taboo.name")
        .description("L10n.Localizable.Widget.Taboo.desc")
        .supportedFamilies([.systemSmall, .systemMedium])
    }
}

struct ScreenTobooWidget: Widget {
    let kind: String = "ScreenTobooWidget"

    var body: some WidgetConfiguration {
        StaticConfiguration(kind: kind, provider: ScreenProvider()) { entry in
            ScreenView(entry: entry, style: .taboo)
        }
        .configurationDisplayName("")
        .description("")
        .supportedFamilies([.accessoryRectangular])
    }
}

struct ScreenFestivalWidget: Widget {
    let kind: String = "ScreenFestivalWidget"

    var body: some WidgetConfiguration {
        StaticConfiguration(kind: kind, provider: ScreenProvider()) { entry in
            ScreenView(entry: entry, style: .festival)
        }
        .configurationDisplayName("")
        .description("")
        .supportedFamilies([.accessoryRectangular])
    }
}
